#include "tickmanager.h"

#include "itick.h"

namespace net {

TickManager::TickManager() {
    tick_ = -1;
}

TickManager::~TickManager() {
}
    
uint32_t TickManager::GetTick() {
    if (tickMap_.empty() || tickMap_.begin()->second.empty()) {
        return -1;
    }
    tick_ = (*tickMap_.begin()->second.begin())->GetTick();
    return tick_;
}

void TickManager::OnTick() {
    Ticks& ticks = tickMap_[tick_];
    for (Ticks::iterator it = ticks.begin(); it != ticks.end(); it++) {
        (*it)->OnTick();
    }
}

void TickManager::AddTick(ITick* tick) {
    tickMap_[tick->GetTick()].push_back(tick);
}

void TickManager::RemoveTick(ITick* tick) {
    Ticks& ticks = tickMap_[tick->GetTick()];
    Ticks::iterator it = ticks.begin();
    for (; it != ticks.end(); ) {
        if (*it == tick) {
            it = ticks.erase(it);
        }
        else {
            it++;
        }
    }
}

}
